package cn.sjtu.security.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 创建Jwt
 */
@Component
@Slf4j
public class JwtUtils {

    @Value("${my.secretKey}")
    private String secretKey;

    public String createJwt(Integer userId, String username, String[] authList) throws UnsupportedEncodingException {
        // 1. 头部
        HashMap<String, Object> headerClaims = new HashMap<>();
        headerClaims.put("alg", "HS256");
        headerClaims.put("typ", "JWT");

        Date issDate = new Date();
        Date expDate = new Date(issDate.getTime() + 1000 * 60 * 60 * 2);

        return JWT.create()
                .withHeader(headerClaims)
                .withIssuer("hescene") // 设置签发人
                .withIssuedAt(issDate) // 签发时间
                .withExpiresAt(expDate)
                .withClaim("userId", userId)
                .withClaim("username", username)
                .withArrayClaim("userAuth", authList)
                .sign(Algorithm.HMAC256(secretKey));// 使用某种算法 使用SECRET作为密钥
    }

    public boolean verifyToken(String jwtToken){
        try {
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretKey)).build();
            DecodedJWT decodedJWT = jwtVerifier.verify(jwtToken);
            Integer userId = decodedJWT.getClaim("userId").asInt();
            return true;
        } catch (Exception e) {
            log.info("token验证不正确！");
            return false;
        }
    }

    public Integer getUserId(String jwtToken) {
        try {
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretKey)).build();
            DecodedJWT decodedJWT = jwtVerifier.verify(jwtToken);
            Integer userId = decodedJWT.getClaim("userId").asInt();
            return userId;
        } catch (Exception e) {
            log.info("token验证不正确！");
            return -1;
        }
    }


    public String getUsername(String jwtToken) {
        try {
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretKey)).build();
            DecodedJWT decodedJWT = jwtVerifier.verify(jwtToken);
            String username = decodedJWT.getClaim("username").asString();
            return username;
        } catch (Exception e) {
            log.info("token验证不正确！");
            return "";
        }
    }


    public String[] getAuthsList(String jwtToken) {
        try {
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretKey)).build();
            DecodedJWT decodedJWT = jwtVerifier.verify(jwtToken);
            String[] auths = decodedJWT.getClaim("userAuth").asArray(String.class);
            return auths;
        } catch (Exception e) {
            log.info("token验证不正确！");
            return null;
        }
    }
}
